﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Yaml;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace BasicModuleConfiguration.Module
{
    public class ConfigurationModel
    {
        public void Run(string[] args)
        {
            #region Json
            {
                //ConfigurationBuilder builder = new ConfigurationBuilder();
                //builder.AddJsonFile("appsettings.json");
                //IConfiguration configuration = builder.Build();
                //string verification = configuration["verification:AuthService"];
                //string AllowedHosts = configuration["AllowedHosts"];
                //Console.WriteLine($"AuthService:{verification}");
                //Console.WriteLine($"AllowedHosts:{AllowedHosts}");
            }
            #endregion

            #region xml
            {
                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.AddXmlFile("appsettings.xml");
                IConfiguration configuration = builder.Build();
                string verification = configuration["verification:AuthService"];
                string AllowedHosts = configuration["AllowedHosts"];
                Console.WriteLine($"AuthService:{verification}");
                Console.WriteLine($"AllowedHosts:{AllowedHosts}");
            }
            #endregion

            #region Yaml
            {
                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.AddYamlFile("appsettings.yaml");
                IConfiguration configuration = builder.Build();
                string verification = configuration["verification:AuthService"];
                string AllowedHosts = configuration["AllowedHosts"];
                Console.WriteLine($"AuthService:{verification}");
                Console.WriteLine($"AllowedHosts:{AllowedHosts}");
            }
            #endregion

            #region CommandLine
            {
                //dotnet run --AuthService=123456 --AllowedHosts=123456
                foreach (var item in args)
                {
                    Console.WriteLine(item.ToString());
                }
                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.AddCommandLine(args);
                IConfiguration configuration = builder.Build();
                string verification = configuration["AuthService"];
                string AllowedHosts = configuration["AllowedHosts"];
                Console.WriteLine($"AuthServiceCommandLine:{verification}");
                Console.WriteLine($"AllowedHostsCommandLine:{AllowedHosts}");
                //Console.ReadKey();
            }
            #endregion

            #region Txt 自定义配置文件
            {
                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.AddText("appsettings.txt");
                IConfiguration configuration = builder.Build();
                string verification = configuration["verification.AuthService"];
                string AllowedHosts = configuration["AllowedHosts"];
                Console.WriteLine($"AddText:{verification}");
                Console.WriteLine($"AddText:{AllowedHosts}");
            }
            #endregion

            #region json 配置选项
            {
                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.AddJsonFile("appsettings.json", false, true); //动态更新配置服务
                IConfiguration configuration = builder.Build();
                Logging appsettings = new Logging();
                configuration.GetSection("Logging").Bind(appsettings);
                ServiceCollection serviceDescriptors = new ServiceCollection();
                serviceDescriptors.Configure<Logging>(configuration.GetSection("Logging"));
                serviceDescriptors.Configure<Appsettings>( x => x.AllowedHosts = "xxxx");
                var builders = serviceDescriptors.BuildServiceProvider();
                IOptions<Logging> options = builders.GetRequiredService<IOptions<Logging>>();
                IOptions<Appsettings> options2 = builders.GetRequiredService<IOptions<Appsettings>>();
                string verification = options.Value.Default;
                string AllowedHosts = options.Value.MicrosoftAspNetCore;
                Console.WriteLine($"配置选项:{verification}");
                Console.WriteLine($"配置选项:{AllowedHosts}");
            }
            #endregion
        }
    }
}
